<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>客服管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="__STATIC__/admin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="__STATIC__/admin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">客服名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="kefu_name" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">客服分组</label>
                    <div class="layui-input-block">
                        <select name="group_id">
                            <option value="">选择一个分组</option>
                            {foreach name="group" item="vo"}
                            <option value="{$vo['group_id']}">{$vo['group_name']}</option>
                            {/foreach}
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-admin" lay-submit lay-filter="LAY-user-front-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>
        </div>

        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <button class="layui-btn layuiadmin-btn-admin" data-type="add"><i class="layui-icon">&#xe654;</i> 添加</button>
            </div>

            <table id="LAY-user-table" lay-filter="LAY-user-table"></table>
            <script type="text/html" id="avatarTpl">
                <img style="display: inline-block; width: 50%; height: 100%;" src= {{ d.kefu_avatar }}>

            </script>
            <script type="text/html" id="onlineTpl">
                {{#  if(d.online_status == 1){ }}
                <button class="layui-btn layui-btn-xs layui-btn-danger">在线</button>
                {{#  } else { }}
                <button class="layui-btn layui-btn-primary layui-btn-xs">离线</button>
                {{#  } }}
            </script>
            <script type="text/html" id="buttonTpl">
                {{#  if(d.kefu_status == 1){ }}
                <button class="layui-btn layui-btn-xs">已激活</button>
                {{#  } else { }}
                <button class="layui-btn layui-btn-primary layui-btn-xs">已禁用</button>
                {{#  } }}
            </script>
            <script type="text/html" id="table-seller-admin">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
            </script>
        </div>
    </div>
</div>

<script src="__STATIC__/admin/layui/layui.js"></script>
<script>
    layui.config({
        base: '__STATIC__/admin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table'], function(){
        var $ = layui.$
            ,form = layui.form
            ,table = layui.table;

        // 监听搜索
        form.on('submit(LAY-user-table)', function(data){
            var field = data.field;

            // 执行重载
            table.reload('LAY-user-table', {
                where: field
            });
        });

        var active = {

            add: function(){
                layer.open({
                    type: 2
                    ,title: '添加客服'
                    ,content: "{:url('KeFu/addKeFu')}"
                    ,area: ['60%', '80%']
                });
            }
        };

        $('.layui-btn.layuiadmin-btn-admin').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });

    renderTable();
    // 渲染表格
    function renderTable() {

        layui.use(['table', 'form'], function () {
            var $ = layui.$
                ,form = layui.form
                ,table = layui.table;

            table.render({
                elem: "#LAY-user-table",
                url: "/seller/ke_fu/index",
                cols: [
                    [{
                        field: "kefu_code",
                        title: "客服标识"
                    }, {
                        field: "kefu_name",
                        title: "客服名称",
                    }, {
                        field: "kefu_avatar",
                        title: "客服头像",
                        templet: "#avatarTpl",
                    }, {
                        field: "group_name",
                        title: "所属分组",
                    }, {
                        field: "max_service_num",
                        title: "最大服务人数",
                    }, {
                        field: "kefu_status",
                        title: "客服状态",
                        templet: "#buttonTpl",
                    }, {
                        field: "online_status",
                        title: "在线状态",
                        templet: "#onlineTpl",
                    }, {
                        title: "操作",
                        align: "center",
                        fixed: "right",
                        toolbar: "#table-seller-admin"
                    }]
                ],
                page: !0,
                limit: 20,
                height: "full-220",
                text: {
                    none: '暂无相关数据'
                }
            });

            table.on("tool(LAY-user-table)",
                function(e) {
                    if ("del" === e.event) {

                        layer.confirm('您确定要删除该客服？', {
                            title: '友情提示',
                            icon: 3,
                            btn: ['是的', '再想想']
                        }, function(){

                            $.getJSON("/seller/ke_fu/delKeFu/kefu_id/" + e.data.kefu_id, function (res) {
                                if(0 == res.code) {
                                    layer.msg(res.msg);
                                    setTimeout(function () {
                                        renderTable();
                                    }, 300);
                                } else {
                                    layer.msg(res.msg);
                                }
                            });

                        }, function(){

                        });
                    } else if ("edit" === e.event) {

                        layer.open({
                            type: 2,
                            title: "编辑客服",
                            content: "/seller/ke_fu/editKeFu/kefu_id/" + e.data.kefu_id,
                            area: ['60%', '60%']
                        });
                    }
                });

            // 监听搜索
            form.on('submit(LAY-user-front-search)', function(data){
                var field = data.field;

                // 执行重载
                table.reload('LAY-user-table', {
                    where: field
                });
            });
        });
    }
</script>
</body>
</html>
